home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / sviluppo / svilupp2 / gmsppr10.lha / CreateSprites.c < prev    next >
C/C++ Source or Header  |  1996-01-24  |  5KB  |  226 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /* Take a bunch of FORM SPRT files and stuff them into a CAT SPRT    */
  4. /*                                    */
  5. /************************************************************************/
  6.  
  7. #include <proto/iffparse.h>
  8.  
  9. #include <MyLib.h>
  10.  
  11. /************************************************************************/
  12.  
  13. struct IFF
  14. {
  15.   struct IFFHandle *Handle;
  16.   int Open;
  17. };
  18.  
  19. /************************************************************************/
  20.  
  21. struct DosLibrary *DOSBase;
  22. struct Library *IFFParseBase;
  23.  
  24. static struct
  25. {
  26.   char *CAT;
  27.   char **SPRTs;
  28. } Arguments;
  29.  
  30. static struct IFF Source;
  31. static struct IFF Dest;
  32.  
  33. /************************************************************************/
  34.  
  35. static void ErrorClose(void)
  36.  
  37. {
  38.   if (Dest.Handle)
  39.     {
  40.       if (Dest.Open)
  41.     {
  42.       CloseIFF(Dest.Handle);
  43.     }
  44.       if (Dest.Handle->iff_Stream)
  45.     {
  46.       if (!Close(Dest.Handle->iff_Stream))
  47.         {
  48.           PError(0,Arguments.CAT);
  49.         }
  50.     }
  51.       FreeIFF(Dest.Handle);
  52.     }
  53.   if (Source.Handle)
  54.     {
  55.       if (Source.Open)
  56.     {
  57.       CloseIFF(Source.Handle);
  58.     }
  59.       if (Source.Handle->iff_Stream)
  60.     {
  61.       Close(Source.Handle->iff_Stream);
  62.     }
  63.       FreeIFF(Source.Handle);
  64.     }
  65.   DeleteFile(Arguments.CAT);
  66. }
  67.  
  68. /************************************************************************/
  69.  
  70. static int DoWork(void)
  71.  
  72. {
  73.   LONG ErrorCode;
  74.   char **SPRT;
  75.  
  76.   if (!(Dest.Handle=AllocIFF()))
  77.     {
  78.       PError(0,NULL);
  79.       return RETURN_FAIL;
  80.     }
  81.   if (!(Dest.Handle->iff_Stream=Open(Arguments.CAT,MODE_NEWFILE)))
  82.     {
  83.       PError(0,Arguments.CAT);
  84.       return RETURN_FAIL;
  85.     }
  86.   InitIFFasDOS(Dest.Handle);
  87.   if ((ErrorCode=OpenIFF(Dest.Handle,IFFF_WRITE)))
  88.     goto DestError;
  89.   Dest.Open=TRUE;
  90.   if ((ErrorCode=PushChunk(Dest.Handle,MAKE_ID('S','P','R','T'),MAKE_ID('C','A','T',' '),IFFSIZE_UNKNOWN)))
  91.     goto DestError;
  92.   for (SPRT=Arguments.SPRTs; *SPRT; SPRT++)
  93.     {
  94.       if (!(Source.Handle=AllocIFF()))
  95.     {
  96.       PError(0,NULL);
  97.       return RETURN_FAIL;
  98.     }
  99.       if (!(Source.Handle->iff_Stream=Open(*SPRT,MODE_OLDFILE)))
  100.     {
  101.       PError(0,*SPRT);
  102.       return RETURN_FAIL;
  103.     }
  104.       InitIFFasDOS(Source.Handle);
  105.       if ((ErrorCode=OpenIFF(Source.Handle,IFFF_READ)))
  106.     goto SourceError;
  107.       Source.Open=TRUE;
  108.       if ((ErrorCode=StopChunk(Source.Handle,MAKE_ID('S','P','R','T'),MAKE_ID('F','O','R','M'))))
  109.     goto SourceError;
  110.       do
  111.     {
  112.       switch (ErrorCode=ParseIFF(Source.Handle,IFFPARSE_SCAN))
  113.         {
  114.         case 0:
  115.           {
  116.         struct ContextNode *ContextNode;
  117.         void *Memory;
  118.         ULONG Size;
  119.  
  120.         ContextNode=CurrentChunk(Source.Handle);
  121.         Size=ContextNode->cn_Size-4;
  122.         if ((ErrorCode=PushChunk(Dest.Handle,ContextNode->cn_Type,ContextNode->cn_ID,Size+4)))
  123.           goto DestError;
  124.         if (!(Memory=AllocMem(Size,0)))
  125.           {
  126.             PError(0,*SPRT);
  127.             return RETURN_FAIL;
  128.           }
  129.         ErrorCode=ReadChunkBytes(Source.Handle,Memory,Size);
  130.         if (ErrorCode!=Size)
  131.           {
  132.             if (ErrorCode>=0)
  133.               ErrorCode=IFFERR_READ;
  134.             FreeMem(Memory,Size);
  135.             goto SourceError;
  136.           }
  137.         ErrorCode=WriteChunkBytes(Dest.Handle,Memory,Size);
  138.         FreeMem(Memory,Size);
  139.         if (ErrorCode!=Size)
  140.           {
  141.             if (ErrorCode>=0)
  142.               ErrorCode=IFFERR_WRITE;
  143.             goto DestError;
  144.           }
  145.         if ((ErrorCode=PopChunk(Dest.Handle)))
  146.           goto DestError;
  147.           }
  148.           break;
  149.  
  150.         case IFFERR_EOF:
  151.           break;
  152.  
  153.         default:
  154.           goto SourceError;
  155.         }
  156.     }
  157.       while (ErrorCode!=IFFERR_EOF);
  158.       CloseIFF(Source.Handle);
  159.       Source.Open=FALSE;
  160.       Close(Source.Handle->iff_Stream);
  161.       Source.Handle->iff_Stream=MKBADDR(NULL);
  162.       FreeIFF(Source.Handle);
  163.       Source.Handle=NULL;
  164.     }
  165.   if ((ErrorCode=PopChunk(Dest.Handle)))    /* SPRT */
  166.     goto DestError;
  167.   CloseIFF(Dest.Handle);
  168.   Dest.Open=FALSE;
  169.   if (!Close(Dest.Handle->iff_Stream))
  170.     {
  171.       Dest.Handle->iff_Stream=MKBADDR(NULL);
  172.       PError(0,Arguments.CAT);
  173.       return RETURN_FAIL;
  174.     }
  175.   Dest.Handle->iff_Stream=MKBADDR(NULL);
  176.   SetProtection(Arguments.CAT,FIBF_EXECUTE);
  177.   return RETURN_OK;
  178.  
  179.  SourceError:
  180.   FPrintf(StdErr,"IFF error %ld on file %s\n",ErrorCode,*SPRT);
  181.   return RETURN_FAIL;
  182.  
  183.  DestError:
  184.   FPrintf(StdErr,"IFF error %ld on file %s\n",ErrorCode,Arguments.CAT);
  185.   return RETURN_FAIL;
  186. }
  187.  
  188. /************************************************************************/
  189.  
  190. int AmigaMain(void)
  191.  
  192. {
  193.   int RC;
  194.  
  195.   RC=RETURN_CATASTROPHY;
  196.   if ((DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",ROM_VERSION)))
  197.     {
  198.       RC=RETURN_FAIL;
  199.       ErrorHandle();
  200.       if ((IFFParseBase=OpenLibrary("iffparse.library",36)))
  201.     {
  202.       struct RDArgs *RDArgs;
  203.  
  204.       if ((RDArgs=ReadArgs("CATFILE/A,SPRTFILES/A/M",(LONG *)&Arguments,NULL)))
  205.         {
  206.           if ((RC=DoWork()))
  207.         {
  208.           ErrorClose();
  209.         }
  210.           FreeArgs(RDArgs);
  211.         }
  212.       else
  213.         {
  214.           PError(0,NULL);
  215.         }
  216.       CloseLibrary(IFFParseBase);
  217.     }
  218.       else
  219.     {
  220.       VFPrintf(StdErr,"Unable to open iffparse.library V36 or newer.\n",NULL);
  221.     }
  222.       CloseLibrary(&DOSBase->dl_lib);
  223.     }
  224.   return RC;
  225. }
  226.